<?php
namespace Modules\Admin\Controllers;

use S\Request;
use Modules\Admin\Model\Data\User as DataUser;
use Modules\Admin\Model\Service\User as ServiceUser;
use Modules\Admin\Model\Service\Access\Control as ServiceAccessControl;
use Base\Exception\Controller as Exception;

/**
 * @name 管理系统登录
 */
class Login extends Controller {
	protected $sys_view = true;

	/**
	 * @name 登录
	 * @throws Exception
	 */
	public function loginAction() {
		$username = $this->getParams("username");
		$password = $this->getParams("password");

		$userinfo = (new ServiceUser())->loginVerify($username, $password);

		$_SESSION = array(
			'uid' => $userinfo['uid'],
			'uname' => $userinfo['uname'],
			'name' => $userinfo['nick'],
			'actionlist' => ServiceAccessControl::accessibleAclData($userinfo['uid']),
			'groups' => (new DataUser())->getGroupByUid($userinfo['uid']),
			'isadmin' => $userinfo['isadmin'] ? true : false, // 超级管理员标识
			'blocked' => $userinfo['status'] ? false : true, // 禁用状态
		);
		$this->response['msg'] = '登录成功';
	}

	/**
	 * @name 登录页
	 * @format html
	 */
	public function indexAction() {
		if(Request::session('uid')){     //判断是否有session值,如果存在则跳转到后台欢迎页面
			header('location:'.APP_ADMIN_PATH.'/welcome/index');
		}
		$refer = Request::server('HTTP_REFERER');
		if(strpos($refer, APP_ADMIN_PATH)){
			$this->response['refer'] = $refer;
		}
	}

	/**
	 * @name 登出
	 */
	public function logoutAction() {
		session_destroy();
		setcookie('menu', '', time()-1, APP_ADMIN_PATH);
		header('Location: '.APP_ADMIN_PATH.'/welcome/index');
	}

	/**
	 * @name 跳转到登录页
	 */
	public function goto_loginAction() {
		session_destroy();
		setcookie('menu', '', time()-1, APP_ADMIN_PATH);
		header('Location: '.APP_ADMIN_PATH.'/login/index');
	}
}
